package com.zh.web;

import com.github.pagehelper.PageInfo;
import com.zh.po.Order;
import com.zh.po.Product;
import com.zh.po.User;
import com.zh.po.UserProduct;
import com.zh.service.OrderService;
import com.zh.service.ProductService;
import com.zh.service.UserProductService;
import com.zh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class JspController {
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserProductService userProductService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/index")
    public String index(){
        return  "index";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        request.getSession().setAttribute("username", null);
        return  "login";
    }

    /**
     * @description: 用户登录
     * @author ふわふわ
     * @date: 2021/5/11 16:45
     */
    @RequestMapping("user/login")
    public String login(String loginName, String password,HttpServletRequest request){
        User user = userService.getUserByLoginName(loginName);
        if (user == null){
            request.setAttribute("msg","用户名或密码错误");
            return "login";
        }
        if(user.getUserPassword().equals(password)) {
            request.getSession().setAttribute("username", user.getUserName());
            return "index";
        } else{
            request.setAttribute("msg","用户名或密码错误");
            return "login";
        }

    }

    @RequestMapping("/register")
    public String register(){
        return  "register";
    }

    @RequestMapping("/cart")
    public String cart(HttpServletRequest request){
        PageInfo<Product> products = productService.findAllProductCart(1,100,1);
        request.getSession().setAttribute("cart",products);
        return  "cart";
    }

    /**
     * @description: 商品详细界面
     * @return: java.lang.String
     * @author ふわふわ
     * @date: 2021/5/11 16:44
     */
    @RequestMapping("/productInfo/{productId}")
    public String productInfo(@PathVariable Integer productId, HttpServletRequest request, HttpServletResponse response){
        Product productById = productService.getProductById(productId);
        if (productById==null){
            return "index";
        }
        request.setAttribute("product", productById);
        return  "product_info";
    }

    @RequestMapping("/addCart/{userId}/{productId}/{productBuyNum}")
    public String addCart(HttpServletRequest request,@PathVariable Integer userId,@PathVariable Integer productId,@PathVariable Integer productBuyNum) throws IOException, ServletException {
        UserProduct userProduct = userProductService.findByProductId(userId,productId);//查询购物车是否有该商品
        Double productPrice = productService.getProductById(productId).getProductPrice();//商品价格
        if (userProduct != null ){//购物车有该商品
            Integer productBuyNum1 = userProduct.getProductBuyNum();//已在购物车数量
            Integer finalNum=productBuyNum1+productBuyNum;
            userProduct.setProductBuyNum(finalNum);//购买数量叠加
            userProduct.setSubtotal(finalNum*productPrice);//小计
            userProductService.updateCart(userProduct);
        }else {
            userProductService.addShoppingCart(new UserProduct(userId,productId,productBuyNum,productBuyNum*productPrice));
        }
        Product productById = productService.getProductById(productId);
        productById.setProductNum(productById.getProductNum()-productBuyNum);
        productService.updateProduct(productById);
        PageInfo<Product> products = productService.findAllProductCart(1,100,userId);
        request.getSession().setAttribute("cart",products);
        return  "cart";
    }

    @RequestMapping("/cart/{pageNum}/{pageSize}/{userId}")
    public String pageCart(HttpServletRequest request, @PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable Integer userId ){
        PageInfo<Product> products = productService.findAllProductCart(pageNum,pageSize,userId);
        request.getSession().setAttribute("cart",products);
        return "cart";
    }

    @RequestMapping("/order")
    public String order(HttpServletRequest request){
        PageInfo<Order> order = orderService.page(1, 100);
        request.getSession().setAttribute("order",order);
        return  "order";
    }
}
